package com.hpeng.basic.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.hpeng.basic.dao.CategoryDao;
import com.hpeng.basic.service.CategoryManager;
import com.hpeng.sys.model.AssetCard;
import com.hpeng.sys.model.Category;
import com.hpeng.util.Pagination;



@Component("categoryManager")
public class CategoryManagerImpl implements CategoryManager {
	
	@Resource
	private CategoryDao categoryDao;

	public Category addCategory(Category category) {
		return categoryDao.save(category);
	}

	public void deleteCategory(Category category) {
		categoryDao.remove(category.getCatid());
	}

	public List<Category> findCategory() {
		return categoryDao.getAll();
	}

	public Category findCategoryById(String id) {
		return categoryDao.get(id);
	}

	public void updateCategory(Category category) {
		categoryDao.update(category);
	}

	public void setCategoryDao(CategoryDao categoryDao) {
		this.categoryDao = categoryDao;
	}

	public Pagination<Category> getPagination(int pageSize, int pageNumber, String[] hql, String order) {
		return categoryDao.getPagination(pageSize, pageNumber, hql, order);
	}

	public boolean executeBySql(String sql) throws Exception {
		return categoryDao.executeBySql(sql);
	}

	public void deleteAllCategory(String... ids) {
		categoryDao.removeAllByIds(ids);		
	}

	public Category findCategoryByHql(String hql) {
		List<Category> cats = categoryDao.getByHql(hql);
		if(cats.size()==0){
			return null;
		}else{
			return cats.get(0);
		}
	}

	public String[] isLastLevelCategory(String[] stringArray) {
		String[] lastLevelId = new String[stringArray.length];//用于保存还有末级节点的类别Id
		int i = 0;
		for(String id : stringArray){
			if(id.length()<=6){
				String hql = " from Category c where c.catid like('"+id+"%')";
				List<Category> cats = categoryDao.getByHql(hql);
				if(cats.size() == 1){
					lastLevelId[i] = id;
					i++;
				}
			}else{
				lastLevelId[i] = id;
				i++;
			}
		}
		String[] temp = new String[i];
		for(int j = 0; j<temp.length; j++){
			temp[j] = lastLevelId[j];
		}
		return temp;
	}

	public String[] getNoUse(String[] stringArray) {
		String[] temp = new String[stringArray.length];
		int i = 0;
		for(String str : stringArray){
			String sql = "select * from Assetcard a where a.catid = ?";
			List<AssetCard> assetcards = categoryDao.getAssetCardBySql(sql,new String[]{str});
			if(assetcards.isEmpty()){
				temp[i] = str;
				i++;
			}
		}
		String[] ids = new String[i];
		for(int j=0; j<ids.length; j++){
			ids[j] = temp[j];
		}
		return ids;
	}

	public Category autoAddCategory(Category category) {
		String strCatid = category.getCatid();
		//资产类别长度Id为1 添加一级分类
		if(strCatid.length() == 1){
			String hql = " from Category where catid like ('%') and rownum = 1 order by catid desc ";
			Category tempCategory = findCategoryByHql(hql);
			if(tempCategory == null){
				tempCategory = new Category();
				tempCategory.setCatid("000");
			}
			strCatid = numberIncrement(tempCategory.getCatid().substring(0,3));
			//将自增后的Id重新加上去
			category.setCatid(strCatid);
			category = addCategory(category);
			//资产类别Id长度为3 添加二级分类
		}else if(strCatid.length() == 3){
			String hql = " from Category where catid like ('"+strCatid+"%') and rownum = 1 order by catid desc";
			Category tempCategory = findCategoryByHql(hql);
			//如果为第一条数据
			if(tempCategory.getCatid().length()==3){
				strCatid = tempCategory.getCatid() + "001";
			}else{
				strCatid = tempCategory.getCatid().substring(0,3) + numberIncrement(tempCategory.getCatid().substring(3,6));
			}
			//将自增后的Id重新加上去
			category.setCatid(strCatid);			
			category = addCategory(category);
			
			//资产类别Id长度为6 添加三级分类
		}else if(strCatid.length() == 6){
			String hql = " from Category where catid like ('"+strCatid+"%') and rownum = 1 order by catid desc";
			Category tempCategory = findCategoryByHql(hql);
			//如果为第一条数据
			if(tempCategory.getCatid().length()==6){
				strCatid = tempCategory.getCatid() + "0001";
			}else{
				strCatid = tempCategory.getCatid().substring(0,6) + numberIncrement(tempCategory.getCatid().substring(6,10));
			}
			//将自增后的Id重新加上去
			category.setCatid(strCatid);
			category = addCategory(category);
		}
		return category;
	}

	private String numberIncrement(String number){
		int tempNumber = Integer.parseInt("1"+number);
		int afterIncrementNumber = tempNumber+1;
		return Integer.toString(afterIncrementNumber).substring(1);
	}
}
